package Cau9;

import java.awt.Graphics;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Server {

	static DatagramSocket socket;
	static DatagramPacket packet;
	static DataPacket data = new DataPacket();
	static drawPanel dpane = new drawPanel();
	public static class drawPanel extends JPanel {
		
		private static final long serialVersionUID = 1L;
		public drawPanel(){	
			
		}
		public void paintComponent(Graphics g){	
			for (Rectangle rec : data.getRectangles()) {
				rec.drawRectangle(g);
			}
			for (Oval oval : data.getOvals()){
				oval.drawOval(g);
			}
			for (Line line : data.getLines()){
				line.drawLine(g);
			}
			for (Triangle triangle : data.getTriangles()){
				triangle.drawTriangle(g);
			}
			
		}
	
	
	}
	public static void main(String[] args) throws SocketException {
		JFrame server = new JFrame("Server");
		server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		server.setBounds(150,150,500,400);
		server.setVisible(true);
		server.add(dpane);
		RecievePacket();

	}
	private static void RecievePacket() throws SocketException {
		socket = new DatagramSocket(1280);
		int size = 8192;
		while(true){
			byte[] buffer = new byte[size];
			packet = new DatagramPacket(buffer, buffer.length);
			try{
				socket.receive(packet);
				ByteArrayInputStream bais = new ByteArrayInputStream(packet.getData());
				ObjectInputStream ois = new ObjectInputStream(bais);
				data = (DataPacket)ois.readObject();
				dpane.repaint();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

}
